package dm.ast.model.gen.factory;

import dm.sql.comp.ASTComplex;
import test.utility.Randomly;

import java.util.ArrayList;

public class UnionQueryModelFactory extends BaseModelFactory{
    //此处仅随机确定集合查询包含的集合运算符数目
    //以及分配每个select子句的深度和宽度


    public static ComplexityModel unionModels(int wid_t , int dep_t){
        int queryNums = Randomly.getInteger(1 , FactoryConstant.MAX_UNION_OPERATORS + 1);
        ComplexityModel model = BaseModelFactory.defaultModel(wid_t - queryNums , dep_t , queryNums + 1);
        // model 里面的comps[unionOps + 1]
        return testModel(model,dep_t,wid_t);
    }

    public static ComplexityModel testModel(ComplexityModel model,int dep_t,int wid_t){
        int sum = 0;
        int widtNoEqualOne = 0;
        for(int i=0;i< model.count;i++){
            if(model.comps[i].wid_t != 1 && model.comps[i].dep_t == dep_t){
                sum++;
            }
        }
        if(sum==0){
            //重新生成
            unionModels(wid_t, dep_t);
        }else{
            for(int i=0;i< model.count;i++) {
                if (model.comps[i].wid_t == 1) {
                    model.comps[i] = null;
                }else{
                    widtNoEqualOne++;
                }

            }
            ASTComplex[] complexs = new ASTComplex[widtNoEqualOne];
            int j = 0;
            for (int i = 0; i < model.count; i++) {
                if(model.comps[i] != null){
                    complexs[j] = model.comps[i];
                    j++;
                }
            }
            model.comps = complexs;
            model.count = widtNoEqualOne;

        }
        return model;
    }
}
